package com.vunguyen.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.vunguyen.model.Bomon;
import com.vunguyen.model.Canbo;
import com.vunguyen.model.Congvan;
import com.vunguyen.model.Congvanden;
import com.vunguyen.model.Giangvien;
import com.vunguyen.model.Lichlamviec;
import com.vunguyen.model.Phancongcongvanden;
import com.vunguyen.model.PhancongcongvandenBomon;
import com.vunguyen.model.PhancongcongvandenGiangvien;


@Repository
public class CongVanDAOImpl implements CongVanDAO{
    
	@Autowired
	private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
	@Override
    public List<Congvan> getCongVan() {
    	return sessionFactory.getCurrentSession().createQuery("From Congvan").list();
        
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<Congvanden> getCongVanDen() {
		return sessionFactory.getCurrentSession().createQuery("From Congvanden").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Phancongcongvanden> getPhancongcongvanden() {
		return sessionFactory.getCurrentSession().createQuery("From Phancongcongvanden").list();
	}

	@Override
	public void addCongVan(Congvan congvan) {
		
		Session session = this.sessionFactory.getCurrentSession();
		session.save(congvan);

		
	}

	@Override
	public void addCongVanDen(Congvanden congvanden) {
		Session session = this.sessionFactory.getCurrentSession();
		session.save(congvanden);
	}

	@Override
	public Congvan getCongvanById(String idcongvan) {
		Congvan congvan;
		congvan = (Congvan) sessionFactory.getCurrentSession().createQuery("from Congvan where idCongVan='"+idcongvan+"'").list().get(0);

		return congvan;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Congvan> getListCongvanById(String idcongvan) {
		return sessionFactory.getCurrentSession().createQuery("from Congvan where idCongVan='"+idcongvan+"'").list();

	}
	@Override
	public void xoaCongVan(String idcongvan) {	
		Session session = this.sessionFactory.getCurrentSession();
        Congvan cv = (Congvan) session.load(Congvan.class, idcongvan);
        if(cv != null ){
            session.delete(cv);
        }
	}

	@Override
	public void removeCongVanDen(String idcongvan) {
		
		Session session = this.sessionFactory.getCurrentSession();
        Congvanden cvd = (Congvanden) session.load(Congvan.class, idcongvan);
        if(cvd != null ){
            session.delete(cvd);
        }
	}

	@Override
	public void updateCongVan(Congvan congvan, String idCongVan) {
		Session session = this.sessionFactory.getCurrentSession();
		Congvan cv = (Congvan) session.get(Congvan.class, idCongVan);
		cv.setTrichYeu(congvan.getTrichYeu());
		cv.getCongvanden().setNoiGui(congvan.getCongvanden().getNoiGui());
		cv.getCongvanden().setNgayNhan(congvan.getCongvanden().getNgayNhan());
		session.update(cv);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Giangvien> getGiangvien(int idcanbo) {
		return sessionFactory.getCurrentSession().createQuery("from Giangvien where idCanBo <>'"+idcanbo+"'").list();
	}

	@Override
	public void addPhanCongCongVanDen(Phancongcongvanden phancongcongvanden) {
		Session session = this.sessionFactory.getCurrentSession();
		session.save(phancongcongvanden);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Bomon> getBoMon() {
		return sessionFactory.getCurrentSession().createQuery("From Bomon").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Phancongcongvanden> getPhanCongbyId(String idcongvanden) {

		return sessionFactory.getCurrentSession().createQuery("from Phancongcongvanden where idCongVanDen ='"+idcongvanden+"'").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Phancongcongvanden> getPhanCongbyId(String idcongvanden,
			String nguoiphancong) {
		return sessionFactory.getCurrentSession().createQuery("from Phancongcongvanden where idCongVanDen ='"+idcongvanden+"' and nguoiPhanCong='"+nguoiphancong+"'").list();
	}

	@Override
	public void updatePhanCong(Phancongcongvanden phancongcongvanden,
			int idphancong) {
		Session session = this.sessionFactory.getCurrentSession();
		Phancongcongvanden pccvd = (Phancongcongvanden) session.get(Phancongcongvanden.class, idphancong);
		pccvd.setHanCuoi(phancongcongvanden.getHanCuoi());
		pccvd.setXacNhanBcn(phancongcongvanden.getXacNhanBcn());
		session.update(pccvd);

	}

	@Override
	public void xacNhanHoanTat(Phancongcongvanden phancongcongvanden,
			int idphancong) {
		Session session = this.sessionFactory.getCurrentSession();
		Phancongcongvanden pccvd = (Phancongcongvanden) session.get(Phancongcongvanden.class, idphancong);
		pccvd.setXacNhanHoanTat(phancongcongvanden.getXacNhanHoanTat());
		session.update(pccvd);
		
	}

	@Override
	public void themPhancongcongvandenGiangvien(
			PhancongcongvandenGiangvien phancongcongvandenGiangvien) {
		Session session = this.sessionFactory.getCurrentSession();
		session.save(phancongcongvandenGiangvien);
		
	}

	@Override
	public void themPhancongcongvandenBomon(
			PhancongcongvandenBomon phancongcongvandenBomon) {
		Session session = this.sessionFactory.getCurrentSession();
		session.save(phancongcongvandenBomon);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PhancongcongvandenGiangvien> layPhancongcongvandenGiangvien() {
		return sessionFactory.getCurrentSession().createQuery("From PhancongcongvandenGiangvien").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PhancongcongvandenBomon> layPhancongcongvandenBomon() {
		return sessionFactory.getCurrentSession().createQuery("From PhancongcongvandenBomon").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Congvanden> getCongvandenById(String idcongvanden) {
		return sessionFactory.getCurrentSession().createQuery("from Congvanden where idCongVanDen='"+idcongvanden+"'").list();
	}

	@Override
	public Canbo getCanBoById(int idcanbo) {
		Canbo canbo;
		canbo = (Canbo) sessionFactory.getCurrentSession().createQuery("from Canbo where idCanBo='"+idcanbo+"'").list().get(0);
		return canbo;
	}

	@Override
	public int countCongvanden() {
		return sessionFactory.getCurrentSession().createQuery("From Congvanden").list().size();
	}

	@Override
	public int countCongvandi() {
		return sessionFactory.getCurrentSession().createQuery("From Congvandi").list().size();
	}

	@Override
	public List<?> getTest() {
		return sessionFactory.getCurrentSession().createQuery("from Phancongcongvanden pc").list();
	}
    
}
